package org.baojie.raft.util;

/**
 * @author baojie
 * @description: 字符串工具类, 内部封装其他工具类
 * @date 2021/12/8上午11:42
 */
public class StrUtil {

    // ASCII码一览表，ASCII码对照表
    // http://c.biancheng.net/c/ascii/

    private static volatile StrUtil instance;

    private StrUtil() {

    }

    public static final StrUtil getInstance() {
        if (null != instance) {
            return instance;
        } else {
            synchronized (StrUtil.class) {
                if (null == instance) {
                    instance = new StrUtil();
                }
            }
        }
        return instance;
    }

    public final boolean isUpperCase(char c) {
        if (c > 64 && c < 91) {
            return true;
        } else {
            return false;
        }
    }

    public final boolean isUpperCase(String str) {
        if (null == str) {
            return false;
        } else {
            int len = str.length();
            for (int i = 0; i < len; i++) {
                char c = str.charAt(i);
                if (isLowerCase(c)) {
                    return false;
                }
            }
            return true;
        }
    }

    public final boolean isDigit(char c) {
        if (c > 47 && c < 58) {
            return true;
        } else {
            return false;
        }
    }

    public final boolean isDigit(String str) {
        if (null == str) {
            return false;
        } else {
            int len = str.length();
            if (len <= 0) {
                return false;
            }
            char head = str.charAt(0);
            char tail = str.charAt(len - 1);
            // 首尾不能出现 _ 但是中间可以任意个
            if (head == '_' || tail == '_') {
                return false;
            }
            for (int i = 0; i < len; i++) {
                char c = str.charAt(i);
                // 适配 123_123 这种情况
                if (c == '_') {
                    continue;
                }
                // 如果还不是数字类型
                // 那么直接返回false
                if (!isDigit(c)) {
                    return false;
                }
            }
            return true;
        }
    }

    public final boolean isLowerCase(char c) {
        if (c > 96 && c < 123) {
            return true;
        } else {
            return false;
        }
    }

    public final boolean isLowerCase(String str) {
        if (null == str) {
            return false;
        } else {
            int len = str.length();
            for (int i = 0; i < len; i++) {
                char c = str.charAt(i);
                if (isUpperCase(c)) {
                    return false;
                }
            }
            return true;
        }
    }

}
